package com.ruoyi.web.controller.activity;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.activity.domain.ActTeacher;
import com.ruoyi.activity.service.IActTeacherService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 教师信息Controller
 * 
 * @author YZD
 * @date 2022-07-09
 */
@Controller
@RequestMapping("/activity/teacher")
public class ActTeacherController extends BaseController {
    private String prefix = "activity/teacher";

    @Autowired
    private IActTeacherService actTeacherService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysPostService postService;

    @Autowired
    private SysPasswordService passwordService;

    @Autowired
    private ISysDeptService deptService;

    @RequiresPermissions("activity:teacher:view")
    @GetMapping()
    public String teacher()
    {
        return prefix + "/teacher";
    }

    /**
     * 查询教师信息列表
     */
    @RequiresPermissions("activity:teacher:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ActTeacher actTeacher)
    {
        startPage();
        List<ActTeacher> list = actTeacherService.selectActTeacherList(actTeacher);
        for (ActTeacher teacher : list) {
            teacher.setSysUser(userService.selectUserById(teacher.getSysUserId()));
        }
        return getDataTable(list);
    }

    /**
     * 导出教师信息列表
     */
    @RequiresPermissions("activity:teacher:export")
    @Log(title = "教师信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ActTeacher actTeacher)
    {
        List<ActTeacher> list = actTeacherService.selectActTeacherList(actTeacher);
        for (ActTeacher teacher:list){
            SysUser user = userService.selectUserById(teacher.getSysUserId());
            teacher.setSysUser(user);
            teacher.setSysDept(deptService.selectDeptById(user.getDeptId()));
        }
        ExcelUtil<ActTeacher> util = new ExcelUtil<ActTeacher>(ActTeacher.class);
        return util.exportExcel(list, "教师信息数据");
    }

    /**
     * 新增教师信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("roles", roleService.selectRoleAll());
        mmap.put("posts", postService.selectPostAll());
        return prefix + "/add";
    }

    /**
     * 新增保存教师信息
     */
    @RequiresPermissions("activity:teacher:add")
    @Log(title = "教师信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addSave(@Validated ActTeacher actTeacher)
    {
        SysUser user = actTeacher.getSysUser();
        if (UserConstants.USER_NAME_NOT_UNIQUE.equals(userService.checkLoginNameUnique(user.getLoginName()))) {
            return error("新增用户'" + user.getLoginName() + "'失败，登录账号已存在");
        } else if (UserConstants.USER_PHONE_NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
            return error("新增用户'" + user.getLoginName() + "'失败，手机号码已存在");
        } else if (UserConstants.USER_EMAIL_NOT_UNIQUE.equals(userService.checkEmailUnique(user))) {
            return error("新增用户'" + user.getLoginName() + "'失败，邮箱账号已存在");
        }
        user.setSalt(ShiroUtils.randomSalt());
        user.setPassword(passwordService.encryptPassword(user.getLoginName(), user.getPassword(), user.getSalt()));
        //设置修改时间
        user.setCreateTime(new Date());
        user.setCreateBy(ShiroUtils.getLoginName());
        //把账号类型设置为教师类型
        user.setUserType("03");
        userService.insertUser(user);
        actTeacher.setSysUserId(userService.selectOidById());
        return toAjax(actTeacherService.insertActTeacher(actTeacher));
    }

    /**
     * 修改教师信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ActTeacher actTeacher = actTeacherService.selectActTeacherById(id);
        actTeacher.setSysUser(userService.selectUserById(actTeacher.getSysUserId()));
        mmap.put("actTeacher", actTeacher);
        mmap.put("roles",roleService.selectRolesByUserId(actTeacher.getSysUserId()));
        mmap.put("posts",postService.selectPostsByUserId(actTeacher.getSysUserId()));
        return prefix + "/edit";
    }

    /**
     * 修改保存教师信息
     */
    @RequiresPermissions("activity:teacher:edit")
    @Log(title = "教师信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ActTeacher actTeacher)
    {
        SysUser user = actTeacher.getSysUser();
        //校验用户是否允许操作
        userService.checkUserAllowed(user);
        if (UserConstants.USER_PHONE_NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
            return error("修改用户'" + user.getLoginName() + "'失败，手机号码已存在");
        } else if (UserConstants.USER_EMAIL_NOT_UNIQUE.equals(userService.checkEmailUnique(user))) {
            return error("修改用户'" + user.getLoginName() + "'失败，邮箱账号已存在");
        }
        user.setUpdateTime(new Date());
        user.setUpdateBy(ShiroUtils.getLoginName());
        userService.updateUser(user);
        return toAjax(actTeacherService.updateActTeacher(actTeacher));
    }

    /**
     * 删除教师信息
     */
    @RequiresPermissions("activity:teacher:remove")
    @Log(title = "教师信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        for (String tid : Convert.toStrArray(ids)){
            userService.deleteUserById(actTeacherService.selectActTeacherById(Long.valueOf(tid)).getSysUserId());
        }
        return toAjax(actTeacherService.deleteActTeacherByIds(ids));
    }
}
